package com.ruoyi.order.service.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.order.domain.TbProductOrder;
import com.ruoyi.order.domain.TbProductOrderDetail;
import com.ruoyi.order.mapper.TbProductOrderDetailMapper;
import com.ruoyi.order.mapper.TbProductOrderMapper;
import com.ruoyi.order.service.ITbProductOrderDetailService;
import com.ruoyi.order.vo.OrderDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-24
 */
@Service
public class TbProductOrderDetailServiceImpl implements ITbProductOrderDetailService
{
    @Autowired
    private TbProductOrderDetailMapper tbProductOrderDetailMapper;
    @Autowired
    private TbProductOrderMapper tbProductOrderMapper;

    /**
     * 查询订单详情
     * 
     * @param id 订单详情主键
     * @return 订单详情
     */
    @Override
    public TbProductOrderDetail selectTbProductOrderDetailById(Long id)
    {
        return tbProductOrderDetailMapper.selectTbProductOrderDetailById(id);
    }

    /**
     * 查询订单详情列表
     * 
     * @param tbProductOrderDetail 订单详情
     * @return 订单详情
     */
    @Override
    public List<TbProductOrderDetail> selectTbProductOrderDetailList(TbProductOrderDetail tbProductOrderDetail)
    {
        return tbProductOrderDetailMapper.selectTbProductOrderDetailList(tbProductOrderDetail);
    }

    @Override
    public List<OrderDetailVo> listVo(OrderDetailVo orderDetailVo) {
        return tbProductOrderDetailMapper.listByDetailVo(orderDetailVo);
    }

    /**
     * 新增订单详情
     * 
     * @param tbProductOrderDetail 订单详情
     * @return 结果
     */
    @Override
    public int insertTbProductOrderDetail(TbProductOrderDetail tbProductOrderDetail)
    {
        tbProductOrderDetail.setCreateTime(DateUtils.getNowDate());
        return tbProductOrderDetailMapper.insertTbProductOrderDetail(tbProductOrderDetail);
    }

    /**
     * 修改订单详情
     * 
     * @param tbProductOrderDetail 订单详情
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor =  Exception.class)
    public int updateTbProductOrderDetail(TbProductOrderDetail tbProductOrderDetail)
    {
        tbProductOrderDetail.setUpdateTime(DateUtils.getNowDate());
        tbProductOrderDetailMapper.updateTbProductOrderDetail(tbProductOrderDetail);
        // 修改成功后 更新订单的整体报价
        TbProductOrderDetail tbProductOrderDetail1 = tbProductOrderDetailMapper.selectTbProductOrderDetailById(tbProductOrderDetail.getId());
        String orderId = tbProductOrderDetail1.getOrderId();

        TbProductOrderDetail param = new TbProductOrderDetail();
        param.setOrderId(orderId);
        List<TbProductOrderDetail> tbProductOrderDetails = tbProductOrderDetailMapper.selectTbProductOrderDetailList(param);
        BigDecimal orderSumPrice = new BigDecimal("0");
        for (TbProductOrderDetail tbProductOrderDetail2 : tbProductOrderDetails) {
            orderSumPrice = orderSumPrice.add(tbProductOrderDetail2.getSumPrice());
        }

        TbProductOrder tbProductOrder = tbProductOrderMapper.selectTbProductOrderByOrderId(orderId);
        tbProductOrder.setSumPrice(orderSumPrice);
        return tbProductOrderMapper.updateTbProductOrder(tbProductOrder);
    }

    /**
     * 批量删除订单详情
     * 
     * @param ids 需要删除的订单详情主键
     * @return 结果
     */
    @Override
    public int deleteTbProductOrderDetailByIds(Long[] ids)
    {
        return tbProductOrderDetailMapper.deleteTbProductOrderDetailByIds(ids);
    }

    /**
     * 删除订单详情信息
     * 
     * @param id 订单详情主键
     * @return 结果
     */
    @Override
    public int deleteTbProductOrderDetailById(Long id)
    {
        return tbProductOrderDetailMapper.deleteTbProductOrderDetailById(id);
    }
}
